﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace RpgMVX_SavesManager
{
    static class Utils
    {
        public static string boolToString(bool val)
        {
            return val ? "1" : "0";
        }

        public static bool stringToBool(string val)
        {
            return val == "1" ? true : false;
        }

        public static bool isValidString(string val)
        {
            return !string.IsNullOrEmpty(val) && !string.IsNullOrWhiteSpace(val);
        }

        public static string[] getFiles(string path, string ext)
        {
            List<string> ls = new List<string>();
            DirectoryInfo folder = new DirectoryInfo(path);
            foreach (FileInfo file in folder.GetFiles("*" + ext))
            {
                ls.Add(file.Name);
            }
            return ls.ToArray();
        }

        public static bool isGameFolder(string path)
        {
            FileUtils fu = new FileUtils(path);
            return fu.isFolder() && fu.contains("Game.exe","Game.rgss2a","Game.ini");
        }

        public static bool isValidPath(string path)
        {
            return File.Exists(path) || Directory.Exists(path);
        }

        public static string openFolderSelect()
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            if (DialogResult.OK == fbd.ShowDialog())
            {
                //MessageBox.Show(fbd.SelectedPath + isValidString(fbd.SelectedPath) + isValidPath(fbd.SelectedPath));
                return isValidString(fbd.SelectedPath) && isValidPath(fbd.SelectedPath) ? fbd.SelectedPath : "-1";
            }
            return "-1";
        }

        public static string jsonBuilder(SortedList<string,string> lss)
        {
            StringBuilder stb = new StringBuilder("{");
            foreach (string key in lss.Keys)
            {
                stb.Append("\"" + key + "\":");
                stb.Append(lss[key]+",");
            }
            stb.Append("}");
            return stb.ToString().Trim();
        }

        public static string nameParser(string name)
        {
            return nameParser(name, Setting.namingPatternG);
        }

        public static string nameParser(string name, string pattern)
        {
            string[] tags = new string[] {
                "[文件名]","[时间","[编号]","[后缀]"
            };
            pattern.Replace("[文件名]", name);
            return "";
        }
    }

    class IconHelper
    {
        [System.Runtime.InteropServices.DllImport("shell32.dll")]
        private static extern int ExtractIconEx(string lpszFile, int niconIndex, IntPtr[] phiconLarge, IntPtr[] phiconSmall, int nIcons);
        private string exe, path;
        private IntPtr[] largeIcons, smallIcons;
        public bool isValid = false;
        private string iniContent = "[.ShellClassInfo]\r\nIconIndex=MainIcon\r\nIconFile=ico.ico";
        public IconHelper(string exe, string path)
        {
            if (!(new FileInfo(exe).Extension == "exe") || !Utils.isValidPath(path)) return;
            this.exe = exe;
            this.path = path;
            isValid = true;
        }
        public bool execute()
        {
            if (!isValid) return isValid;
            StreamWriter fs = new StreamWriter(path + @"\desktop.ini");
            fs.Write(iniContent);
            fs.Close();
            FileInfo fi = new FileInfo(path + @"\desktop.ini");
            fi.Attributes |= FileAttributes.Hidden;
            fi.Attributes |= FileAttributes.System;
            var appPath = path;
            int IconCount = ExtractIconEx(appPath, -1, null, null, 0);
            largeIcons = new IntPtr[IconCount];
            smallIcons = new IntPtr[IconCount];
            ExtractIconEx(appPath, 0, largeIcons, smallIcons, IconCount);
            var imageList1 = new ImageList();
            var listview1 = new ListView();
            for (int i = 0; i < IconCount; i++)
            {
                imageList1.Images.Add(Icon.FromHandle(largeIcons[i])); 
                ListViewItem lvi = new ListViewItem();
                lvi.ImageIndex = i;
                listview1.Items.Add(lvi);
            }
            for (int i = 0; i < listview1.Items.Count; i++)
            {
                System.IO.FileStream fsi = new System.IO.FileStream(path + "\\ico.ico", System.IO.FileMode.Create);
                imageList1.Images[listview1.Items[i].ImageIndex].Save(fsi, System.Drawing.Imaging.ImageFormat.Icon);
                fs.Close();
                fi = new FileInfo(path + "\\ico.ico");
                fi.Attributes |= FileAttributes.Hidden;
            }
            return isValid;
        }
    }

    class FileUtils
    {
        public string path;
        public bool isExist;
        public FileUtils(string path)
        {
            this.path = path;
            isExist = Utils.isValidPath(path) || isFolder();
        }

        public bool isFolder()
        {
            return Directory.Exists(path);
        }

        public bool isFile()
        {
            return !isFolder() && isExist;
        }

        public void copyTo(string destFolder)
        {
            if (!Utils.isValidPath(destFolder)) Directory.CreateDirectory(destFolder);
            if (isFile()) File.Copy(path, destFolder);
            else if(isFolder())
            {
                foreach(var f in Directory.EnumerateFileSystemEntries(path))
                {
                    
                }
            }
        }

        public void rename(string name)
        {
            if (isFile())
            {
                File.Move(path, new FileInfo(path).DirectoryName + "\\" + name);
                return;
            }
            List<string> s = path.Split('\\').ToList();
            s.RemoveAt(s.Count - 1);
            if (isFolder()) Directory.Move(path, string.Join("\\", s.ToArray()) + "\\" + name);
        }

        public bool contains(params string[] file)
        {
            if (!isFolder()) return false;
            foreach (var i in file)
            {
                if (Directory.EnumerateFileSystemEntries(path).Contains(i)) continue;
                return false;
            }
            return true;
        }

        public void delete()
        {
            if (isFile())
            {
                File.Delete(path);
            }
            else if (isFolder())
            {
                Directory.Delete(path);
            }
        }
    }
}
